
package com.xxl.job.core.commons.annotation.impl;


import com.xxl.job.core.commons.annotation.ChineseCheck;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description : 名字中文校验实现
 * @Author : wangqiangqiang
 * @Date : 11:35 2019/4/27
 * @Email : wangqiangqiang@cetcbigdata.com
 */
public class ChineseCheckImpl implements ConstraintValidator<ChineseCheck, String> {
    @Override
    public void initialize(ChineseCheck constraintAnnotation) {
    }

    @Override
    public boolean isValid(String name, ConstraintValidatorContext context) {
        // 如果为空，则不作校验
        if (null == name) {
            return true;
        }
        // 检测字符串是否含有中文
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(name);
        if (m.find()) {
            return false;
        }
        return true;
    }
}
